package datalookin.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.whu.zj.MyApplication;
import com.whu.zj.R;

import java.util.ArrayList;
import java.util.List;

import datalookin.activity.MainWaterActivity;
import datalookin.activity.MainWaterActivity2;
import datalookin.adapter2.RealStationAdapter;
import datalookin.dto2.StationAllDataDto;
import datalookin.network2.GetRealStationDataAction;
import inter.Delegate;

/**
 * Created by Administrator on 2016/6/5.
 */
public class RealStationFragment extends Fragment {

    ListView lv_real;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_realstation,null);
        initview(view);
        return view;
    }

    private void initview(View view) {
        lv_real = (ListView) view.findViewById(R.id.id_lv_historyplant);
        lv_real.setAdapter(realStationAdapter);
    }

    List<StationAllDataDto> stationAllDataDtos = new ArrayList<StationAllDataDto>();
    RealStationAdapter realStationAdapter = new RealStationAdapter();

    GetRealStationDataAction getRealStationDataAction = new GetRealStationDataAction();
    Delegate realStationResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("[")) return;
            stationAllDataDtos = JSON.parseArray(response,StationAllDataDto.class);
            realStationAdapter.setStationAllDataDtos(stationAllDataDtos);
            realStationAdapter.notifyDataSetChanged();
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };
    {
        getRealStationDataAction.setDelegate(realStationResponse);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MainWaterActivity2 mainWaterActivity = (MainWaterActivity2) getActivity();
//        getRealStationDataAction.setWaterPlantCode(MyApplication.mobileUserDto.getWaterUser().getWaterplantNum());
        getRealStationDataAction.setWaterPlantCode(mainWaterActivity.getWaterPlantNum());
        getRealStationDataAction.get();
    }

    @Override
    public void onStart() {
        super.onStart();

    }
}
